package com.dlc.felear.xiaoensale.constant;

import android.content.Context;
import android.util.Log;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.dao.FlavourSpeedDao;
import com.dlc.felear.xiaoensale.entity.ActionStepItem;
import com.dlc.felear.xiaoensale.entity.FlavourSpeed;
import com.dlc.felear.xiaoensale.entity.Operator;
import com.dlc.felear.xiaoensale.entity.SfStInfo;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.utils.UuidUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TempData {
    private static final int yttl_serial_YOU = 1;
    private static final int yttl_serial_SC = 2;
    private static final int yttl_serial_CU = 3;
    private static final int yttl_serial_XY = 4;
    private static final int yttl_serial_LC = 5;
    private static final int yttl_serial_HY = 6;
    private static final int yttl_serial_SHUI = 7;
    private static final int getYttl_serial_JLA = 8;
    private static final int yttl_serial_JLA=12;
    private static final int yttl_serial_YAN = 0;
    private static final int yttl_serial_TANG = 1;
    private static final int yttl_serial_JJ = 2;
    private static Context context;

    public static void init(Context tempContext) {
        context = tempContext;
    }


    private static int getRamdom(int size) {
        Random random = new Random();
        return random.nextInt(size);
    }

    public static List<ActionStepItem> getActionStepItemData() {
        List<ActionStepItem> list = new ArrayList<>();
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_TZJR, R.drawable.cc_icon, 0));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_KQJR, R.drawable.cc_1_icon, 1));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_GSGZ, R.drawable.gg_on, 2));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_DKGZ, R.drawable.gg_off, 3));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_TZJB, R.drawable.xz_off_icon, 4));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_KSJB, R.drawable.xz_on_icon, 5));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_XG, R.drawable.xg_icon, 6));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_JSC, R.drawable.jsc_icon, 7));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_DC, R.drawable.dc_icon, 8));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_FW, R.drawable.reset_icon, 9));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_CCJD, R.drawable.reset_icon, 10));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_JC, R.drawable.reset_icon, 11));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_DS, R.drawable.reset_icon, 13));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_CCSC, R.drawable.reset_icon, 14));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_CCSC, R.drawable.reset_icon, 15));
        list.add(new ActionStepItem(Constant.ACTION_STEP_GDZ, Constant.ACTION_NAME_JGG, R.drawable.reset_icon, 16));
        return list;
    }

    public static List<ActionStepItem> getActionStepItemData1() {
        List<ActionStepItem> list = new ArrayList<>();
//        list.add(new ActionStepItem(Constant.ACTION_STEP_JGT, Constant.ACTION_NAME_Y, R.drawable.setting_1_icon, 10));
        Log.e("qindaji", JsonHelper.toJson(list));
        return list;
    }

    public static List<ActionStepItem> getActionStepItemData2() {
        List<ActionStepItem> list = new ArrayList<>();
        list.add(new ActionStepItem(Constant.ACTION_STEP_JYT, Constant.ACTION_NAME_YOU, R.drawable.setting_1_icon, yttl_serial_YOU));
        list.add(new ActionStepItem(Constant.ACTION_STEP_JYT, Constant.ACTION_NAME_HHJY, R.drawable.setting_1_icon, yttl_serial_SC));
        list.add(new ActionStepItem(Constant.ACTION_STEP_JYT, Constant.ACTION_NAME_GQZ, R.drawable.setting_1_icon, yttl_serial_CU));
        list.add(new ActionStepItem(Constant.ACTION_STEP_JYT, Constant.ACTION_NAME_YTJJ, R.drawable.setting_1_icon, yttl_serial_XY));
        list.add(new ActionStepItem(Constant.ACTION_STEP_JYT, Constant.ACTION_NAME_LAOCHOU, R.drawable.setting_1_icon, yttl_serial_LC));
        list.add(new ActionStepItem(Constant.ACTION_STEP_JYT, Constant.ACTION_NAME_HONGYOU, R.drawable.setting_1_icon, yttl_serial_HY));
        list.add(new ActionStepItem(Constant.ACTION_STEP_JYT, Constant.ACTION_NAME_SHUI, R.drawable.setting_1_icon, yttl_serial_SHUI));
        list.add(new ActionStepItem(Constant.ACTION_STEP_JYT, Constant.ACTION_NAME_JLA, R.drawable.setting_1_icon, yttl_serial_JLA));
        return list;
    }

    public static List<Operator> getDefaultOperater(FlavourSpeedDao flavourSpeedDao) {
        String id = "";
        List<Operator> tempList = new ArrayList<>();
        //抽风开 5档
        tempList.add(new Operator(1, Constant.ACTION_NAME_DKGZ, id, Constant.ACTION_STEP_GDZ, 255, 255, 1));
        //加热开 5档 70度
        tempList.add(new Operator(3, Constant.ACTION_NAME_KQJR, id, Constant.ACTION_STEP_GDZ, 5, 70, 1));
        //搅拌开 5档
        tempList.add(new Operator(2, Constant.ACTION_NAME_KSJB, id, Constant.ACTION_STEP_GDZ, 7, 7, 1));
        //加油 150ml
        tempList.add(getTiaoLiaoOperator(4, Constant.ACTION_NAME_YOU, Constant.ACTION_STEP_JYT, 50, id,flavourSpeedDao));
        //打开格子3
        tempList.add(new Operator(5, Constant.ACTION_NAME_JSC, id, Constant.ACTION_STEP_GDZ, 4, 20, 20));
        //炒菜10秒
        tempList.add(new Operator(6, Constant.ACTION_NAME_CCJD, id, Constant.ACTION_STEP_GDZ, 1, 1, 10));
        //打开格子2
        tempList.add(new Operator(7, Constant.ACTION_NAME_JSC, id, Constant.ACTION_STEP_GDZ, 2, 20, 20));
        //炒菜10秒
        tempList.add(new Operator(3, Constant.ACTION_NAME_CCSC, id, Constant.ACTION_STEP_GDZ, 0, 60, 1));
        //打开格子1
        tempList.add(new Operator(9, Constant.ACTION_NAME_JSC, id, Constant.ACTION_STEP_GDZ, 1, 20, 20));
        //加热开 7档 90度
        tempList.add(new Operator(3, Constant.ACTION_NAME_KQJR, id, Constant.ACTION_STEP_GDZ, 7, 90, 1));
        tempList.add(new Operator(12, Constant.ACTION_NAME_CCJD, id, Constant.ACTION_STEP_GDZ, 1, 1, 20));
        //炒菜时长
        tempList.add(new Operator(3, Constant.ACTION_NAME_CCSC, id, Constant.ACTION_STEP_GDZ, 0, 0, 1));
        //加盐水
        tempList.add(getTiaoLiaoOperator(13, Constant.ACTION_NAME_HONGYOU, Constant.ACTION_STEP_JYT, 15, id,flavourSpeedDao));
        //加生抽 20ml
        tempList.add(getTiaoLiaoOperator(14, Constant.ACTION_NAME_YTJJ, Constant.ACTION_STEP_JYT, 15, id,flavourSpeedDao));
        //停止加热
        tempList.add(new Operator(18, Constant.ACTION_NAME_TZJR, id, Constant.ACTION_STEP_GDZ, 0, 0, 1));
        //停止搅拌
        tempList.add(new Operator(18, Constant.ACTION_NAME_TZJB, id, Constant.ACTION_STEP_GDZ, 0, 0, 10));
        //停止搅拌
        tempList.add(new Operator(18, Constant.ACTION_NAME_GSGZ, id, Constant.ACTION_STEP_GDZ, 0, 0, 1));

        //重新排序
        for (int i = 0; i < tempList.size(); i++) {
            Operator operator = tempList.get(i);
            tempList.remove(i);
            operator.setNum(i + 1);
            tempList.add(i, operator);
        }
        return tempList;
    }

    /**
     * 获取调料操作步骤
     *
     * @param num
     * @param name
     * @param type
     * @param value1
     * @return
     */
    private static Operator getTiaoLiaoOperator(int num, String name, int type, int value1, String id, FlavourSpeedDao flavourSpeedDao) {
        Operator operator = new Operator();
        try {
            FlavourSpeed flavourSpeed = flavourSpeedDao.queryForName(name);
            if (flavourSpeed != null) {
                operator.setNum(num);
                operator.setName(flavourSpeed.getName());
                operator.setType(type);
                operator.setmId(id);
                operator.setValue(flavourSpeed.getDoNum());
                operator.setValue1(value1);
            }
        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
        }
        return operator;
    }


    private static float combineFloatValue(float size, FlavourSpeed flavourSpeed) {

        float tempvalue = 0f;
        try {
            if (flavourSpeed == null || size <= 0) {
                return 1f;
            }
            tempvalue = size * flavourSpeed.getStdData() + flavourSpeed.getBaseData();
            Log.e("qindada", " StdData=" + flavourSpeed.getStdData() + " BaseData=" + flavourSpeed.getBaseData() + " size=" + size);

        } catch (Exception e) {
            Log.e("qindada", e.getMessage());
        }
        return tempvalue;
    }

    private static float valueToTime(int value, FlavourSpeed flavourSpeed) {

        float tempvalue = value;
        try {
            if (flavourSpeed == null || value <= 0 || flavourSpeed.getSpeed() <= 0) {
                return value;
            }
            tempvalue = (float) Math.ceil(value / flavourSpeed.getSpeed());
        } catch (Exception e) {
            Log.e("qindada", e.getMessage());
        }
        return tempvalue;
    }


    public static List<SfStInfo> getSfItem1(int addr) {
        List<SfStInfo> list = new ArrayList<>();
        list.add(new SfStInfo(addr, 0, "报警信息"));
        list.add(new SfStInfo(addr, 0, "清除报警"));
        list.add(new SfStInfo(addr, 0, "电机正转"));
        list.add(new SfStInfo(addr, 0, "电机反转"));
        list.add(new SfStInfo(addr, 0, "设置原点"));
        list.add(new SfStInfo(addr, 0, "重置编码器"));
        list.add(new SfStInfo(addr, 1, "运行指定点"));
        return list;
    }

    public static List<String> getMenuType() {
        List<String> list = new ArrayList<>();
        list.add("M260");
        list.add("M400");
        list.add("M600");
        list.add(Constant.ZHENGCAI);
        list.add(Constant.JRCP);
        return list;
    }


    public static List<FlavourSpeed> getFlavourDatas() {
        List<FlavourSpeed> list = new ArrayList<>();
        try {
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.ACTION_NAME_WDBCXH),
                    Constant.ACTION_NAME_WDBCXH, 0, 1f, 0));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_GT_1),
                    Constant.ACTION_NAME_Y, 1, 1.03f, yttl_serial_YAN));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_GT_2),
                    Constant.ACTION_NAME_T, 1, 1f, yttl_serial_TANG));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_GT_3),
                    Constant.ACTION_NAME_JJ, 1, 1f, yttl_serial_JJ));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_YT_1),
                    Constant.ACTION_NAME_YOU, 2, 20f, yttl_serial_YOU));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_YT_2),
                    Constant.ACTION_NAME_HHJY, 2, 20f, yttl_serial_SC));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_YT_3),
                    Constant.ACTION_NAME_GQZ, 2, 20f, yttl_serial_CU));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_YT_4),
                    Constant.ACTION_NAME_YTJJ, 2, 20f, yttl_serial_XY));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_YT_5),
                    Constant.ACTION_NAME_LAOCHOU, 2, 20f, yttl_serial_LC));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_YT_6),
                    Constant.ACTION_NAME_HONGYOU, 2, 20f, yttl_serial_HY));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_YT_7),
                    Constant.ACTION_NAME_SHUI, 2, 20f, yttl_serial_SHUI));
            list.add(new FlavourSpeed(UuidUtil.getUuidByName(Constant.TIAOLIAO_YT_8),
                    Constant.ACTION_NAME_JLA, 2, 20f, getYttl_serial_JLA));
            Log.e("TempData", JsonHelper.toJson(list));
        } catch (Exception e) {
            Log.e("TempData", e.getMessage());
        }

        return list;
    }

    public static Operator btyeCmdToOperator(ActionStepItem actionStepItem, byte[] data) {
        Operator operator = new Operator();
        operator.setType(actionStepItem.getType());
        operator.setName("请选择");
        Log.e("TempData", "recordOperater data:" + JsonHelper.toJson(actionStepItem));
        switch (data[1]) {
            case (byte) 0x21:
                if (data[3] == 0 || data[4] == 0) {
                    operator.setName(Constant.ACTION_NAME_TZJR);
                } else {
                    operator.setName(Constant.ACTION_NAME_KQJR);
                }
                operator.setValue(data[3]);
                operator.setValue1(data[4]);
                operator.setDelayTime(1);
                break;
            case (byte) 0x18:
                if (data[3] == 0 || data[4] == 0) {
                    operator.setName(Constant.ACTION_NAME_GSGZ);
                    operator.setValue(0);
                    operator.setValue1(0);
                } else {
                    operator.setName(Constant.ACTION_NAME_DKGZ);
                    operator.setValue(255);
                    operator.setValue1(255);
                }
                operator.setDelayTime(1);
                break;
            case (byte) 0x11:
                if (data[3] == 0) {
                    operator.setName(Constant.ACTION_NAME_TZJB);
                } else {
                    operator.setName(Constant.ACTION_NAME_KSJB);
                }
                operator.setValue(data[3]);
                operator.setValue1(data[3]);
                operator.setDelayTime(1);
                break;
            case (byte) 0x22:
                operator.setName(Constant.ACTION_NAME_CCJD);
                operator.setValue(data[3]);
                operator.setValue1(data[3]);
                operator.setDelayTime(20);
                break;
            case (byte) 0x14:
                if (data[3] > 0 && data[3] < 5) {
                    operator.setName(Constant.ACTION_NAME_JSC);
                    operator.setValue(data[3]);
                    operator.setValue1(20);
                    operator.setDelayTime(20);
                }
                break;
            case (byte) 0x23:
                if (data[3] > 0) {
                    operator.setName(Constant.ACTION_NAME_JC);
                    operator.setValue(data[3]);
                    operator.setValue1(data[3]);
                    operator.setDelayTime(data[3]);
                }
                break;
            case (byte) 0x16:
                if (data[4] > 0) {
                    operator.setName(actionStepItem.getName());
                    operator.setValue(data[3]);
                    operator.setValue1(data[4]);
                    operator.setDelayTime(data[4]);
                }
                break;
            case (byte) 0x17:
                int temp = converValueTo32(data, 4);
                Log.e("TempData", "液体调料:" + temp + "毫秒");
                if (temp > 0) {
                    operator.setName(actionStepItem.getName());
                    operator.setValue(data[3]);
                    operator.setValue1(temp / 1000);
                    operator.setDelayTime(temp / 1000);
                }
                break;
        }
        Log.e("TempData", "Operater:" + JsonHelper.toJson(operator));
        return operator;
    }

    /**
     * 拼接数据
     *
     * @return
     */
    private static int converValueTo32(byte[] src, int offset) {
        int value;
        value = (int) ((src[offset] & 0xFF)
                | ((src[offset + 1] & 0xFF) << 8)
                | ((src[offset + 2] & 0xFF) << 16)
                | ((src[offset + 3] & 0xFF) << 24));
        return value;
    }
}
